package org.cn.leroy.service.serviceImpl;

import org.cn.leroy.dao.UserInfoDao;
import org.cn.leroy.entity.UserInfo;
import org.cn.leroy.service.UserInfoService;
import org.cn.leroy.util.BaseTools;
import org.cn.leroy.util.PageTemplet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Leroy on 2016/11/7.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoDao userInfoDao;
    private PageTemplet pageTemplet;
    public List<UserInfo> queryAll() {
        List<UserInfo> list = new ArrayList<UserInfo>();
        list = userInfoDao.queryList();
        return list;
    }

    public List<UserInfo> selectByPage(int page,int length){
     //   int length = pageTemplet.getLength();
        int totalPage = this.getTotalPage(length);
        int offset = (page - 1)*length;
        pageTemplet.setOffset(offset);
        pageTemplet.setLength(length);
        pageTemplet.setTotal(totalPage);
        List<UserInfo> result = userInfoDao.selectByPage(pageTemplet);
        return result;
    }

    public UserInfo queryById(String id) {
        UserInfo info = userInfoDao.queryById(id);
        return info;
    }

    public void insert(UserInfo user) {
//        if(!BaseTools.isNumber(user.getId())){
//            System.out.println("非数字或为空！");
//        }
//        userInfoDao.insert(user);
    }

    public void update(UserInfo user) {
        userInfoDao.update(user);
    }

    public void delete(String id) {
        userInfoDao.delete(id);
    }

    public int getTotalPage(int length){
        List<UserInfo> list = this.queryAll();
        int counts = list.size();
        int totalPage = counts%length == 0 ? counts/length : counts/length+1;
        return totalPage;
    }
}
